package com.groupThree.service;

import com.groupThree.bean.StateOfAU;
import com.groupThree.bean.User;
import com.groupThree.dao.BaseUserDao;
import com.groupThree.dao.imp.UserDaoMySql;

import java.util.List;
import java.util.Map;

public class UserService {

    private static BaseUserDao dao = new UserDaoMySql();
    /**
     * 用于控制台的查询
     * @return {size：总数 ,day:新增}
     */
    public static List<Map<String,Integer>> console(){
        return dao.console();
    }

    /**
     * 用于用户列表查询
     * @param limit
     * @param offset
     * @param pageNumber
     * @return
     */

    public static List<User> findAll(boolean limit, int offset, int pageNumber){
        return dao.findAll(limit,offset,pageNumber);
    }

    /**
     * 根据电话号码查找
     * @param u_phone
     * @return  user
     */
    public static User findByU_Phone(String u_phone){
        return dao.findByU_Phone(u_phone);
    }

    /**
     * 录入用户信息
     * @param user
     * @return
     */
     public static StateOfAU insert(User user){
         StateOfAU state = new StateOfAU();
         //录入失败
         if (dao.findSameu_Phone(user.getU_phone())) {
             System.out.println("该手机号码已经被注册!");
             state.setStateAu(1);
             return state;
         }
         if (dao.findSameU_Idcard(user.getU_idcard())) {
             System.out.println("该身份证号码已经存在！");
             state.setStateAu(2);
             return state;
         }
         //录入成功
         dao.insert(user);
         state.setStateAu(0);
         return state;

     }
    public static boolean loginInsert(User user){
        boolean b = dao.loginInsert(user);
        return b;
    }
    /**
     * 根据用户id更改
     * @param u_id
     * @param newUser
     * @return
     */
    public static StateOfAU update(int u_id,User newUser){
        StateOfAU state = new StateOfAU();
        if (newUser.getU_phone() != null ) {
            dao.delete(u_id);
            return insert(newUser);

        } else {
            state.setStateAu(-1);
            return state;
        }
    }

    /**
     * 根据用户id删除
     * @param u_id
     * @return
     */
    public static boolean delete(int u_id){
        return dao.delete(u_id);
    }

    /**
     * 根据手机号删除
     * @param u_phone
     * @return
     */
    public static boolean delete(String u_phone){
        return dao.delete(u_phone);
    }
    /**
     * 更改登录时间
     */
    public static boolean updateLoginTime(String u_phone){
        return dao.updateLoginTime(u_phone);
    }

}
